package 链表;

public class 链表求和 {

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int x=0;  //进位
        ListNode res = new ListNode(0);
        ListNode node=res;
        while (l1!=null ||l2!=null || x!=0){
            int sum=x;
            if (l1!=null){
                sum+=l1.val;
                l1=l1.next;
            }
            if (l2!=null){
                sum+=l2.val;
                l2=l2.next;
            }
            node.next=new ListNode(sum%10);
            x = sum/10;
            node=node.next;
        }
        return res.next;
    }
}
